<%@page import="com.hamizhong.iflowers.web.vo.ProductVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.GoodsTargetMapVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.GoodsFestivalMapVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.GoodsPurposeMapVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.GoodsProductMapVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.GoodsVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.FlowerCountVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.FlowerFloridVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.TargetVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.FestivalVO"%>
<%@page import="com.hamizhong.iflowers.web.vo.PurposeVO"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@include file="../../common/common_admin.jsp" %>
<title>商品更新</title>
<link rel="stylesheet" href="<%=request.getContextPath() %>/kindeditor/themes/default/default.css" />
<link rel="stylesheet" href="<%=request.getContextPath() %>/kindeditor/plugins/code/prettify.css" />
<script charset="utf-8" src="<%=request.getContextPath() %>/kindeditor/kindeditor.js"></script>
<script charset="utf-8" src="<%=request.getContextPath() %>/kindeditor/lang/zh_CN.js"></script>
<script charset="utf-8" src="<%=request.getContextPath() %>/kindeditor/plugins/code/prettify.js"></script>
</head>

<%
	GoodsVO goodsVO = (GoodsVO)request.getAttribute("goodsVO");
	List<GoodsProductMapVO> goodsProductMapVOList = goodsVO.getGoodsProductMapVOs();
	List<GoodsPurposeMapVO> goodsPurposeMapVOList = goodsVO.getGoodsPurposeMapVOs();
	List<GoodsFestivalMapVO> goodsFestivalMapVOList = goodsVO.getGoodsFestivalMapVOs();
	List<GoodsTargetMapVO> goodsTargetMapVOList = goodsVO.getGoodsTargetMapVOs();
	
	List<PurposeVO> purposeVOList = (List<PurposeVO>)request.getAttribute("purposeVOList");
	List<FestivalVO> festivalVOList = (List<FestivalVO>)request.getAttribute("festivalVOList");
	List<TargetVO> targetVOList = (List<TargetVO>)request.getAttribute("targetVOList");
	List<FlowerFloridVO> flowerFloridVOList = (List<FlowerFloridVO>)request.getAttribute("flowerFloridVOList");
	List<FlowerCountVO> flowerCountVOList = (List<FlowerCountVO>)request.getAttribute("flowerCountVOList");
%>

<script type="text/javascript">
	var editor = null;
	KindEditor.ready(function(K) {
		var editor1 = K.create('textarea[name="goodsDescribe"]', {
			cssPath : '<%=request.getContextPath() %>/kindeditor/plugins/code/prettify.css',
			uploadJson : '<%=request.getContextPath() %>/kindeditor/jsp/upload_json.jsp',
			fileManagerJson : '<%=request.getContextPath() %>/kindeditor/jsp/file_manager_json.jsp',
			allowFileManager : true,
	
			resizeType : -1,
			minWidth: 700,
			minHeight: 200,
			items : [
				'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
				'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
				'insertunorderedlist', '|', 'emoticons', 'image', 'link'],
			afterCreate : function() {
				var self = this;
				K.ctrl(document, 13, function() {
					self.sync();
					document.forms['itemForm'].submit();
				});
				K.ctrl(self.edit.doc, 13, function() {
					self.sync();
					document.forms['itemForm'].submit();
				});
			}
	
		});
		prettyPrint();
		editor = editor1;
	});
	
	function checkFormInfo() {
		var goodsName = $('#goodsName').val();
		var goodsPrice = $('#goodsPrice').val();
		var purposeId = $('input[name="purposeId"]:checked');
		var festivalId = $('input[name="festivalId"]:checked');
		var targetId = $('input[name="targetId"]:checked');
		var flowerFloridId = $('#flowerFloridId').val();
		var flowerCountId = $('#flowerCountId').val();
		var sortId = $('#sortId').val();
		var remarks = $('#remarks').val();
		
		var picFileName1 = $('#picFileName1').val();
		var picFileName2 = $('#picFileName2').val();
		var picFileName3 = $('#picFileName3').val();
		
		//var productIds = $('input[name="productIds"]');
		//商品详细的html
		var goodsDescribe = editor.html();
		//var text = editor.text();

		if(goodsName == null || goodsName == "") {
			alert("请填写商品名称！");
			$('#goodsName').focus();
			return false;
		}
		if(goodsPrice == null || goodsPrice == "") {
			alert("请填写商品价格！");
			$('#goodsPrice').focus();
			return false;
		}
		if(purposeId.length < 1) {
			alert("请至少选择一个用途！");
			return false;
		}
		if(festivalId.length < 1) {
			alert("请至少选择一个节日！");
			return false;
		}
		if(targetId.length < 1) {
			alert("请至少选择一个赠送对象！");
			return false;
		}
		if(flowerFloridId == null || flowerFloridId == "") {
			alert("请选择花语！");
			return false;
		}
		if(flowerCountId == null || flowerCountId == "") {
			alert("请选择商品支数！");
			return false;
		}
		if(sortId == null || sortId == "") {
			alert("请填入排序号！");
			return false;
		}
/*  	if(productIds.length < 1) {
			alert("请至少选择一个产品！");
			return false;
		}
		if(picFileName1 == null || picFileName1 == "" 
				|| picFileName2 == null || picFileName2 == "" 
				|| picFileName3 == null || picFileName3 == "" ) {
			alert("请选择上传完整的若干张商品展示图！！");
			return false;
		} */
		var picUpdateDivInputStr = '';
		if(picFileName1 != null && picFileName1 != "") {
			picUpdateDivInputStr += 'S';
		}
		if(picFileName2 != null && picFileName2 != "") {
			if(picUpdateDivInputStr == '') {
				picUpdateDivInputStr += 'M';
			}else {
				picUpdateDivInputStr += '|M';
			}
		}
		if(picFileName3 != null && picFileName3 != "") {
			if(picUpdateDivInputStr == '') {
				picUpdateDivInputStr += 'L';
			}else {
				picUpdateDivInputStr += '|L';
			}
		}
		$('#modifiedPicType').val(picUpdateDivInputStr);
		if(goodsDescribe == null || goodsDescribe == "") {
			alert("请在编辑器中编辑商品的详细信息！");
			return false;
		}
		return true;
	}
</script>

<body>
	<div class="displayPageHeader">当前位置：产商管理 >> 商品管理>> 商品更新</div>
	<div class="pageDiv">
		<div class="pageHeadDiv"><font>更新商品</font></div>
		<div class="infoDiv">
			<form id="itemForm" onsubmit="return checkFormInfo();" action="<%=request.getContextPath() %>/admin/doGoodsUpdate.action" method="post" enctype="multipart/form-data">
				<input type="text" style="display: none;" id="goodsId" name="goodsId" value="<%=goodsVO.getGoodsId() %>">
				<div class="pageDiv" style="margin-left: 10px;">
					<div style="background-color: #E3EFFB;height: 25px;line-height: 25px;padding-left: 10px;">基本信息</div>
					<div class="infoDiv">
						<table cellpadding="2px">
							<tr>
								<td style="width: 70px;">&nbsp;&nbsp;商品名称&nbsp;&nbsp;</td>
								<td>
									<div style="border-bottom: 1px dashed #999;padding: 5px;">
										<input type="text" name="goodsName" id="goodsName" style="width: 200px;" value="<%=goodsVO.getGoodsName()%>">
									</div>
								</td>
							</tr>
							
							<tr>
								<td style="width: 70px;">&nbsp;&nbsp;商品价格&nbsp;&nbsp;</td>
								<td>
									<div style="border-bottom: 1px dashed #999;padding: 5px;">
										<input type="text" name="goodsPrice" id="goodsPrice" style="width: 200px;" value="<%=goodsVO.getGoodsPrice()%>">
									</div>
								</td>
							</tr>
							
							<tr>
								<td style="width: 70px;">&nbsp;&nbsp;用&nbsp;&nbsp;途&nbsp;&nbsp;</td>
								<td>
									<div style="border-bottom: 1px dashed #999;padding: 5px;">
									<%
										PurposeVO purposeVO = null;
										GoodsPurposeMapVO mapPurposeVO = null;
										for(int i=0;i<purposeVOList.size();i++) {
											purposeVO = purposeVOList.get(i);
											boolean isExsit = false;
											for(int j=0;j<goodsPurposeMapVOList.size();j++) {
												mapPurposeVO = goodsPurposeMapVOList.get(j);
												//当当前循环的Item正好在Map中存在时，此时需要选中
												if(mapPurposeVO.getPurposeVO().getPurposeId().equals(purposeVO.getPurposeId())) {
													isExsit = true;
													break;
												}
											}
											if(isExsit) {
											%>
												<%=purposeVO.getPurposeName() %><input checked="checked" type="checkbox" name="purposeId" style="margin-right: 10px;" value="<%=purposeVO.getPurposeId() %>">
											<%
											}else {
											%>
												<%=purposeVO.getPurposeName() %><input type="checkbox" name="purposeId" style="margin-right: 10px;" value="<%=purposeVO.getPurposeId() %>">
											<%	
											}
										}
									%>
									</div>
								</td>
							</tr>
							
							<tr>
								<td style="width: 70px;">&nbsp;&nbsp;节&nbsp;&nbsp;日&nbsp;&nbsp;</td>
								<td>
									<div style="border-bottom: 1px dashed #999;padding: 5px;">
									<%
										FestivalVO festivalVO = null;
										GoodsFestivalMapVO mapFestivalVO = null;
										for(int i=0;i<festivalVOList.size();i++) {
											festivalVO = festivalVOList.get(i);
											boolean isExsit = false;
											for(int j=0;j<goodsFestivalMapVOList.size();j++) {
												mapFestivalVO = goodsFestivalMapVOList.get(j);
												//当当前循环的Item正好在Map中存在时，此时需要选中
												if(mapFestivalVO.getFestivalVO().getFestivalId().equals(festivalVO.getFestivalId())) {
													isExsit = true;
													break;
												}
											}
											if(isExsit) {
											%>
												<%=festivalVO.getFestivalName() %><input checked="checked" type="checkbox" name="festivalId" style="margin-right: 10px;" value="<%=festivalVO.getFestivalId() %>">
											<%
											}else {
											%>
												<%=festivalVO.getFestivalName() %><input type="checkbox" name="festivalId" style="margin-right: 10px;" value="<%=festivalVO.getFestivalId() %>">
											<%	
											}
										}
									%>
									</div>
								</td>
							</tr>
							
							<tr>
								<td style="width: 70px;">&nbsp;&nbsp;赠送对象&nbsp;&nbsp;</td>
								<td>
									<div style="border-bottom: 1px dashed #999;padding: 5px;">
									<%
										TargetVO targetVO = null;
										GoodsTargetMapVO mapTargetVO = null;
										for(int i=0;i<targetVOList.size();i++) {
											targetVO = targetVOList.get(i);
											boolean isExsit = false;
											for(int j=0;j<goodsTargetMapVOList.size();j++) {
												mapTargetVO = goodsTargetMapVOList.get(j);
												//当当前循环的Item正好在Map中存在时，此时需要选中
												if(mapTargetVO.getTargetVO().getTargetId().equals(targetVO.getTargetId())) {
													isExsit = true;
													break;
												}
											}
											if(isExsit) {
											%>
												<%=targetVO.getTargetName() %><input checked="checked" type="checkbox" name="targetId" style="margin-right: 10px;" value="<%=targetVO.getTargetId() %>">
											<%
											}else {
											%>
												<%=targetVO.getTargetName() %><input type="checkbox" name="targetId" style="margin-right: 10px;" value="<%=targetVO.getTargetId() %>">
											<%	
											}
										}
									%>
									</div>
								</td>
							</tr>
							
							<tr>
								<td style="width: 70px;">&nbsp;&nbsp;花&nbsp;&nbsp;语&nbsp;&nbsp;</td>
								<td>									
									<div style="border-bottom: 1px dashed #999;padding: 5px;">
										<select id="flowerFloridId" name="flowerFloridId" style="width: 200px;">
										<%
											for(int i=0;i<flowerFloridVOList.size();i++) {
												FlowerFloridVO flowerFloridVO = flowerFloridVOList.get(i);
												if(flowerFloridVO.getFlowerFloridId() == goodsVO.getFlowerFloridVO().getFlowerFloridId()) {
											%>
												<option selected="selected" value="<%=flowerFloridVO.getFlowerFloridId() %>"><%=flowerFloridVO.getFlowerFloridName() %></option>
											<%
												}else {
											%>
												<option value="<%=flowerFloridVO.getFlowerFloridId() %>"><%=flowerFloridVO.getFlowerFloridName() %></option>
											<%		
												}
											}
										%>
										</select>
									</div>
								</td>
							</tr>
							
							<tr>
								<td style="width: 70px;">&nbsp;&nbsp;支&nbsp;&nbsp;数&nbsp;&nbsp;</td>
								<td>
									<div style="border-bottom: 1px dashed #999;padding: 5px;">
										<select id="flowerCountId" name="flowerCountId" style="width: 200px;">
										<%
											for(int i=0;i<flowerCountVOList.size();i++) {
												FlowerCountVO flowerCountVO = flowerCountVOList.get(i);
												if(flowerCountVO.getFlowerCountId() == goodsVO.getFlowerCountVO().getFlowerCountId()) {
											%>
												<option selected="selected" value="<%=flowerCountVO.getFlowerCountId() %>"><%=flowerCountVO.getFlowerCountName() %></option>
											<%
												}else {
											%>
												<option value="<%=flowerCountVO.getFlowerCountId() %>"><%=flowerCountVO.getFlowerCountName() %></option>
											<%		
												}
											}
										%>
										</select>
									</div>
								</td>
							</tr>
							
							<tr>
								<td style="width: 70px;">&nbsp;&nbsp;排序号&nbsp;&nbsp;</td>
								<td>
									<div style="border-bottom: 1px dashed #999;padding: 5px;">
										<input type="text" name="sortId" id="sortId" style="width: 200px;" value="<%=goodsVO.getSortId()%>">
									</div>
								</td>
							</tr>
							
							<tr>
								<td style="width: 70px;">&nbsp;&nbsp;备&nbsp;&nbsp;注&nbsp;&nbsp;</td>
								<td>
									<div style="border-bottom: 1px dashed #999;padding: 5px;">
										<textarea id="remarks" name="remarks" id="remarks" style="width: 200px;" rows="" cols=""><%=goodsVO.getRemarks() %></textarea>
									</div>
								</td>
							</tr>
						</table>
					</div>
				</div>
			
			 	<div class="pageDiv" style="margin-left: 10px;">
					<div style="background-color: #E3EFFB;height: 25px;line-height: 25px;padding-left: 10px;">产品信息<span style="color: red;">【暂不支持商品组成结构的修改】</span></div>
					<div class="infoDiv">
						<div class="itemListDiv" style="min-height: 150px">
							<table border="1px" id="itemListTable">
								<thead>
									<tr>
										<th width="100">产品ID</th>
										<th width="200">产品名称</th>
										<th width="100">花材</th>
										<th width="100">颜色</th>
										<th width="200">备注</th>
									</tr>
								</thead>
								<%
									for(int i=0;i<goodsProductMapVOList.size();i++) {
										GoodsProductMapVO goodsProductMapVO = goodsProductMapVOList.get(i);
										ProductVO productVO = goodsProductMapVO.getProductVO();
								%>
									<tr>
										<td><%=productVO.getProductId() %></td>
										<td><%=productVO.getProductName() %></td>
										<td><%=productVO.getVarietyVO().getVarietyName() %></td>
										<td><%=productVO.getColorVO().getColorName() %></td>
										<td><%=productVO.getRemarks() %></td>
									</tr>
								<%
									}
								%>
							</table>
						</div>
					</div>
				</div>
				
				<div class="pageDiv" style="margin-left: 10px;">
					<div style="background-color: #E3EFFB;height: 25px;line-height: 25px;padding-left: 10px;">上传图片<span style="color: green;">【此栏中的预览图将进行缩放显示，仅做参考】</span></div>
					<div class="infoDiv">
						<div class="itemListDiv" style="min-height: 150px">
							<table border="1px" id="itemListTable2" cellpadding="0" cellspacing="0" bordercolor="white">
								<tr><td colspan="4"></td></tr>
								<tr height="80px;">
									<td>【列表展示图】[170x170]</td>
									<td>
										<div style="width: 200px;">
											<input type="file" name="upload" id="picFileName1" onchange="previewImage('previewPicDiv1',this,70,70);">
										</div>
									</td>
									<td>
										<div id="previewPicDiv1" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);">
											<img width="70" height="70" alt="" src="<%=request.getContextPath() %>/<%=goodsVO.getGoodsPictureS() %>">
										</div>
									</td>
								</tr>
								<tr height="80px;">
									<td>【商品主大图】[460x460]</td>
									<td>
										<div style="width: 200px;">
											<input type="file" name="upload" id="picFileName2" onchange="previewImage('previewPicDiv2',this,70,70);">
										</div>
									</td>
									<td>
										<div id="previewPicDiv2" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);">
											<img width="70" height="70" alt="" src="<%=request.getContextPath() %>/<%=goodsVO.getGoodsPictureM() %>">
										</div>
									</td>
								</tr>
								<tr height="80px;">
									<td>【商品特写图】[1000x1000]</td>
									<td>
										<div style="width: 200px;">
											<input type="file" name="upload" id="picFileName3" onchange="previewImage('previewPicDiv3',this,70,70);">
										</div>
									</td>
									<td>
										<div id="previewPicDiv3" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);">
											<img width="70" height="70" alt="" src="<%=request.getContextPath() %>/<%=goodsVO.getGoodsPictureL() %>">
										</div>
									</td>
								</tr>
							</table>
							<!-- 用于记录修改过的图片 -->
							<input type="text" style="display: none;" id="modifiedPicType" name="modifiedPicType">
						</div>
						<div class="margin_5"></div>
					</div>
				</div>
				
				<div class="pageDiv" style="margin-left: 10px;">
					<div style="background-color: #E3EFFB;height: 25px;line-height: 25px;padding-left: 10px;">详细描述</div>
					<div class="infoDiv">
						<div class="itemListDiv" style="min-height: 150px">
							<textarea id="goodsDescribe" name="goodsDescribe" cols="100" rows="8" style="width:100%;height:200px;visibility:hidden;">
								<%=goodsVO.getGoodsDescribe()%>
							</textarea>
						</div>
						<div class="margin_5"></div>
					</div>
				</div>
				
				<div class="pageDiv" style="margin-left: 10px;">
					<div style="background-color: #E3EFFB;height: 25px;line-height: 25px;padding-left: 10px;">操作栏</div>
					<div class="infoDiv">
						<input style="margin: 10px 0;" type="button" onclick="closeWindow();" value="取消修改">
						<input style="margin: 10px 0;" type="submit" value="确认修改">
					</div>
				</div>
			</form>
			
			<div class="margin_50"></div>
		</div>
	</div>
</body>

<script type="text/javascript">

function closeWindow() {
	window.close();
}

//验证图片格式
function checkPicture(picFileId){
	var picFileName = document.getElementById(picFileId).value;
	var type = picFileName.substring(picFileName.lastIndexOf(".")+1, picFileName.length).toLowerCase();
	if(type!="jpg"&&type!="bmp"&&type!="gif"&&type!="png"){
		alert("请上传正确的图片格式");
		return false;
	}
	return true;
}

//图片预览
function previewImage(divImage,upload,width,height) {  
	if(checkPicture(upload.id)){
		document.getElementById(divImage).style.display = "block";
		try{
			var picPath; 
			//图片路径     
			//var browser_Agent=navigator.userAgent;
			//判断浏览器的类型(这里暂时只验证有效支持：IE、FF、Chrome)
			if(navigator.appName.indexOf("Microsoft Internet Explorer") != -1 && document.all) {
				var preview = document.getElementById(divImage);
				picPath = upload.value;
				preview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = picPath;
				preview.style.width = width;
				preview.style.height = height;
			}else {
				picPath = window.URL.createObjectURL(upload.files[0]);
				document.getElementById(divImage).innerHTML = "<img id='picPreviewImg' src='"+picPath+"' width='"+width+"' height='"+height+"'/>";
			}
		}catch(e){
			alert("请上传正确的图片格式");
		}
	} 
}

</script>

</html>